import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ConfigService } from '@nestjs/config';
import { BaseService } from '@wwjCommon/base/base.service';
import { CacheService } from '@wwjCommon/cache/cache.service';
import { LoggingService } from '@wwjCommon/logging/logging.service';
import { UploadService } from '@wwjVendor/upload/upload.service';
import { PayService } from '@wwjVendor/pay/pay.service';
import { SmsService } from '@wwjVendor/sms/sms.service';
import { NoticeService } from '@wwjVendor/notice/notice.service';

@Injectable()
export class MemberAccountService extends BaseService<any> {
  private readonly logger = new Logger(MemberAccountService.name);

  constructor(
    @InjectRepository(Object)
    protected readonly repository: Repository<any>,
    private readonly cacheService: CacheService,
    private readonly configService: ConfigService,
    private readonly loggingService: LoggingService,
    private readonly uploadService: UploadService,
    private readonly payService: PayService,
    private readonly smsService: SmsService,
    private readonly noticeService: NoticeService,
  ) {
    super(repository);
  }

  /**
   * getPage
   * 对应 PHP: MemberAccountService_admin::getPage()
   * 逻辑类型: undefined - undefined
   */
  async getPage(where: any[]) {
    // 基于PHP真实逻辑: getPage
    // PHP原文: $field = 'member_account_log.id, member_account_log.member_id, member_account_log.site_id, member_account_log.account_type, member_account_log.account...
const field = 'member_account_log.id, member_account_log.member_id, member_account_log.site_id, member_account_log.account_type, member_account_log.account_data,member_account_log.account_sum, member_account_log.from_type, member_account_log.related_id, member_account_log.create_time, member_account_log.memo';
        const member_where = [];
        if (typeof where[ 'keywords' )) && where[ 'keywords' ] != '') {
            member_where[] = [ "member.member_no|member.nickname|member.mobile", 'like', '%' . this.model.handelSpecialCharacter(where[ 'keywords' ))  + '%' ];
  }

  /**
   * getInfo
   * 对应 PHP: MemberAccountService_admin::getInfo()
   * 逻辑类型: undefined - undefined
   */
  async getInfo(id: number) {
    // 基于PHP真实逻辑: getInfo
    // PHP原文: $field = 'id, member_id, site_id, account_type, account_data, from_type, related_id, create_time, memo';         return $this->model->where([ [ 'id', ...
const field = 'id, member_id, site_id, account_type, account_data, from_type, related_id, create_time, memo';
        return this.model.where([ [ 'id', '=', id ], [ 'site_id', '=', this.site_id ) )).with('memberInfo').field(field).append([ 'from_type_name', 'account_type_name' )).findOrEmpty().toArray(];
    }

    /**
     * 添加调整积分数据
     * @param data
     * @return mixed
     */
    async adjustPoint(data)
    {
        return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], 'point', data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    /**
     * 添加调整余额账户
     * @param data
     * @return bool
     */
    async adjustBalance(data)
    {
        return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], 'balance', data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    async adjustMoney(data)
    {
        return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], MemberAccountTypeDict.MONEY, data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    /**
     * 获取账户类型的变动方式
     * @param account_type
     * @return array|mixed|string
     */
    async getFromType(account_type)
    {
        if (!array_key_exists(account_type, MemberAccountTypeDict.getType())) throw new AdminBusinessException('MEMBER_TYPE_NOT_EXIST'];
        return MemberAccountChangeTypeDict.getType(account_type);
    }

    /**
     * 获取账户数据和
     * @param string account_type (注意查询对应账户)
     */
    async getSumAccount(string account_type)
    {
        return this.model.where([ [ 'site_id', '=', this.site_id ], [ 'account_type', '=', account_type ) )).sum('account_data');
    }

    /**
     * 会员账户详情
     * @param int member_id
     * @return */
    async getMemberAccountInfo(int member_id)
    {
        const field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
        return ( this.memberService ).where([ [ 'member_id', '=', member_id ], [ 'site_id', '=', this.site_id ) )).field(field).findOrEmpty().toArray(];
    }

    /**
     * 已提现佣金
     * @return float
     */
    async getWithdrawnCommission(int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * adjustPoint
   * 对应 PHP: MemberAccountService_admin::adjustPoint()
   * 逻辑类型: undefined - undefined
   */
  async adjustPoint(data: any[]) {
    // 基于PHP真实逻辑: adjustPoint
    // PHP原文: return ( new CoreMemberAccountService() )->addLog($this->site_id, $data[ 'member_id' ], 'point', $data[ 'account_data' ], 'adjust', $data[ 'memo' ]); ...
return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], 'point', data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    /**
     * 添加调整余额账户
     * @param data
     * @return bool
     */
    async adjustBalance(data)
    {
        return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], 'balance', data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    async adjustMoney(data)
    {
        return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], MemberAccountTypeDict.MONEY, data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    /**
     * 获取账户类型的变动方式
     * @param account_type
     * @return array|mixed|string
     */
    async getFromType(account_type)
    {
        if (!array_key_exists(account_type, MemberAccountTypeDict.getType())) throw new AdminBusinessException('MEMBER_TYPE_NOT_EXIST'];
        return MemberAccountChangeTypeDict.getType(account_type);
    }

    /**
     * 获取账户数据和
     * @param string account_type (注意查询对应账户)
     */
    async getSumAccount(string account_type)
    {
        return this.model.where([ [ 'site_id', '=', this.site_id ], [ 'account_type', '=', account_type ) )).sum('account_data');
    }

    /**
     * 会员账户详情
     * @param int member_id
     * @return */
    async getMemberAccountInfo(int member_id)
    {
        const field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
        return ( this.memberService ).where([ [ 'member_id', '=', member_id ], [ 'site_id', '=', this.site_id ) )).field(field).findOrEmpty().toArray(];
    }

    /**
     * 已提现佣金
     * @return float
     */
    async getWithdrawnCommission(int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * adjustBalance
   * 对应 PHP: MemberAccountService_admin::adjustBalance()
   * 逻辑类型: undefined - undefined
   */
  async adjustBalance(data: any[]) {
    // 基于PHP真实逻辑: adjustBalance
    // PHP原文: return ( new CoreMemberAccountService() )->addLog($this->site_id, $data[ 'member_id' ], 'balance', $data[ 'account_data' ], 'adjust', $data[ 'memo' ])...
return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], 'balance', data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    async adjustMoney(data)
    {
        return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], MemberAccountTypeDict.MONEY, data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    /**
     * 获取账户类型的变动方式
     * @param account_type
     * @return array|mixed|string
     */
    async getFromType(account_type)
    {
        if (!array_key_exists(account_type, MemberAccountTypeDict.getType())) throw new AdminBusinessException('MEMBER_TYPE_NOT_EXIST'];
        return MemberAccountChangeTypeDict.getType(account_type);
    }

    /**
     * 获取账户数据和
     * @param string account_type (注意查询对应账户)
     */
    async getSumAccount(string account_type)
    {
        return this.model.where([ [ 'site_id', '=', this.site_id ], [ 'account_type', '=', account_type ) )).sum('account_data');
    }

    /**
     * 会员账户详情
     * @param int member_id
     * @return */
    async getMemberAccountInfo(int member_id)
    {
        const field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
        return ( this.memberService ).where([ [ 'member_id', '=', member_id ], [ 'site_id', '=', this.site_id ) )).field(field).findOrEmpty().toArray(];
    }

    /**
     * 已提现佣金
     * @return float
     */
    async getWithdrawnCommission(int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * adjustMoney
   * 对应 PHP: MemberAccountService_admin::adjustMoney()
   * 逻辑类型: undefined - undefined
   */
  async adjustMoney(data: any[]) {
    // 基于PHP真实逻辑: adjustMoney
    // PHP原文: return ( new CoreMemberAccountService() )->addLog($this->site_id, $data[ 'member_id' ], MemberAccountTypeDict::MONEY, $data[ 'account_data' ], 'adjust...
return ( this.coreMemberAccountService ).addLog(this.site_id, data[ 'member_id' ], MemberAccountTypeDict.MONEY, data[ 'account_data' ), 'adjust', data[ 'memo' )];
    }

    /**
     * 获取账户类型的变动方式
     * @param account_type
     * @return array|mixed|string
     */
    async getFromType(account_type)
    {
        if (!array_key_exists(account_type, MemberAccountTypeDict.getType())) throw new AdminBusinessException('MEMBER_TYPE_NOT_EXIST'];
        return MemberAccountChangeTypeDict.getType(account_type);
    }

    /**
     * 获取账户数据和
     * @param string account_type (注意查询对应账户)
     */
    async getSumAccount(string account_type)
    {
        return this.model.where([ [ 'site_id', '=', this.site_id ], [ 'account_type', '=', account_type ) )).sum('account_data');
    }

    /**
     * 会员账户详情
     * @param int member_id
     * @return */
    async getMemberAccountInfo(int member_id)
    {
        const field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
        return ( this.memberService ).where([ [ 'member_id', '=', member_id ], [ 'site_id', '=', this.site_id ) )).field(field).findOrEmpty().toArray(];
    }

    /**
     * 已提现佣金
     * @return float
     */
    async getWithdrawnCommission(int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * getFromType
   * 对应 PHP: MemberAccountService_admin::getFromType()
   * 逻辑类型: undefined - undefined
   */
  async getFromType(account_type: any) {
    // 基于PHP真实逻辑: getFromType
    // PHP原文: if (!array_key_exists($account_type, MemberAccountTypeDict::getType())) throw new AdminException('MEMBER_TYPE_NOT_EXIST');         return MemberAccoun...
if (!array_key_exists(account_type, MemberAccountTypeDict.getType())) throw new AdminBusinessException('MEMBER_TYPE_NOT_EXIST'];
        return MemberAccountChangeTypeDict.getType(account_type);
    }

    /**
     * 获取账户数据和
     * @param string account_type (注意查询对应账户)
     */
    async getSumAccount(string account_type)
    {
        return this.model.where([ [ 'site_id', '=', this.site_id ], [ 'account_type', '=', account_type ) )).sum('account_data');
    }

    /**
     * 会员账户详情
     * @param int member_id
     * @return */
    async getMemberAccountInfo(int member_id)
    {
        const field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
        return ( this.memberService ).where([ [ 'member_id', '=', member_id ], [ 'site_id', '=', this.site_id ) )).field(field).findOrEmpty().toArray(];
    }

    /**
     * 已提现佣金
     * @return float
     */
    async getWithdrawnCommission(int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * getSumAccount
   * 对应 PHP: MemberAccountService_admin::getSumAccount()
   * 逻辑类型: undefined - undefined
   */
  async getSumAccount(account_type: string) {
    // 基于PHP真实逻辑: getSumAccount
    // PHP原文: return $this->model->where([ [ 'site_id', '=', $this->site_id ], [ 'account_type', '=', $account_type ] ])->sum('account_data');     }      /**      *...
return this.model.where([ [ 'site_id', '=', this.site_id ], [ 'account_type', '=', account_type ) )).sum('account_data');
    }

    /**
     * 会员账户详情
     * @param int member_id
     * @return */
    async getMemberAccountInfo(int member_id)
    {
        const field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
        return ( this.memberService ).where([ [ 'member_id', '=', member_id ], [ 'site_id', '=', this.site_id ) )).field(field).findOrEmpty().toArray(];
    }

    /**
     * 已提现佣金
     * @return float
     */
    async getWithdrawnCommission(int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * getMemberAccountInfo
   * 对应 PHP: MemberAccountService_admin::getMemberAccountInfo()
   * 逻辑类型: undefined - undefined
   */
  async getMemberAccountInfo(member_id: number) {
    // 基于PHP真实逻辑: getMemberAccountInfo
    // PHP原文: $field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';         r...
const field = 'point, point_get, balance, balance_get, growth, growth_get, money, money_get, commission, commission_get, commission_cash_outing';
        return ( this.memberService ).where([ [ 'member_id', '=', member_id ], [ 'site_id', '=', this.site_id ) )).field(field).findOrEmpty().toArray(];
    }

    /**
     * 已提现佣金
     * @return float
     */
    async getWithdrawnCommission(int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * getWithdrawnCommission
   * 对应 PHP: MemberAccountService_admin::getWithdrawnCommission()
   * 逻辑类型: undefined - undefined
   */
  async getWithdrawnCommission(member_id: number) {
    // 基于PHP真实逻辑: getWithdrawnCommission
    // PHP原文: $condition = [             [ 'site_id', '=', $this->site_id ],             [ 'account_type', '=', MemberAccountTypeDict::COMMISSION ],             [ '...
const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', MemberAccountTypeDict.COMMISSION ],
            [ 'from_type', '=', 'cash_out' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

    /**
     * 账户支出总额
     * @return float
     */
    async getExpensesSumAccount(string account_type, int member_id = 0)
    {
        const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }

  /**
   * getExpensesSumAccount
   * 对应 PHP: MemberAccountService_admin::getExpensesSumAccount()
   * 逻辑类型: undefined - undefined
   */
  async getExpensesSumAccount(account_type: number, member_id: number) {
    // 基于PHP真实逻辑: getExpensesSumAccount
    // PHP原文: $condition = [             [ 'site_id', '=', $this->site_id ],             [ 'account_type', '=', $account_type ],             [ 'account_data', '<', ...
const condition = [
            [ 'site_id', '=', this.site_id ],
            [ 'account_type', '=', account_type ],
            [ 'account_data', '<', '0' ]
        ];
        if (!!member_id)) condition[] = [ 'member_id', '=', member_id ];

        return this.model.where(condition).sum('account_data');
    }

}
  }
}
